<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <body>

        <ui:composition template="./permission.xhtml">

            <ui:define name="tabContent">
                <table border="0" cellspacing="5">
                    <tbody>
                        <tr>
                            <td style="vertical-align: top">
                                <h:form>
                                    <p:panel>
                                        <h:panelGrid columns="2">
                                            Hiển thị quyền*    
                                            <p:selectOneRadio  id="criteria" value="#{permissionBean.selectedCriteria}">
                                                <f:selectItem itemLabel="Theo chức năng" itemValue="byResource"/>
                                                <f:selectItem itemLabel="Theo người dùng" itemValue="byUser"/>
                                                <f:selectItem itemLabel="Theo nhóm" itemValue="byRole"/>
                                                <p:ajax event="change" update="selectOne" listener="#{permissionBean.onCriteriaSelect()}"/>
                                            </p:selectOneRadio>

                                            Chọn* 
                                            <p:selectOneMenu id="selectOne" value="#{permissionBean.selectedId}">
                                                <f:selectItems value="#{permissionBean.idMap}"/>
                                            </p:selectOneMenu>
                                            <p:commandButton ajax="false" value="Hiển thị" action="#{permissionBean.viewPermission()}"/>
                                        </h:panelGrid>

                                    </p:panel>
                                </h:form>
                            </td>

                            <td style="vertical-align: top">
                                <h:form rendered="#{permissionBean.editMode == true}">
                                    <p:panel>
                                        <b>Chức năng: </b>#{permissionBean.selected.resourceId.name}
                                        <br/>
                                        <b>Hành động: </b>#{permissionBean.selected.action}
                                        <br/>
                                        <br/>
                                        Gán quyền cho các nhóm sau:
                                        <br/>
                                        <h:selectManyMenu value="#{permissionBean.selected.omapRoleList}" converter="#{omapRoleConverter}"
                                                          style="width: 150px; height: 200px;">
                                            <f:selectItems value="#{permissionBean.allRoles}" var="role" 
                                                           itemLabel="#{role.roleName}" itemValue="#{role}"/>
                                        </h:selectManyMenu>
                                        <br/>
                                        <br/>
                                        Gán quyền cho những người dùng sau: 
                                        <br/>
                                        <h:selectManyMenu value="#{permissionBean.selected.omapUserList}" converter="#{omapUserConverter}"
                                                          style="width: 150px; height: 200px;">
                                            <f:selectItems value="#{permissionBean.allUsers}" var="user" 
                                                           itemLabel="#{user.username}" itemValue="#{user}"/>
                                        </h:selectManyMenu>
                                        <br/>
                                        <p:commandButton ajax="false" value="Áp dụng" action="#{permissionBean.editSelected()}"/>
                                    </p:panel>
                                </h:form>
                            </td>
                        </tr>
                    </tbody>
                </table>


                <br/>
                <h:form>
                    <p:dataTable id="table" value="#{permissionBean.list}" var="item" paginator="#{permissionBean.list.size() >20}" rows="20"
                                 selection="#{permissionBean.selected}" selectionMode="single" resizableColumns="true"
                                 rowKey="#{item.id}">
                        <f:facet name="header">  
                            <div align="left">
                                <p:commandButton ajax="false" value="Chỉnh sửa" icon="ui-icon ui-icon-pencil" action="#{permissionBean.prepareEdit()}"/>
                            </div>
                        </f:facet> 

                        <f:facet name="footer">  
                            <div align="left">
                                <p:commandButton ajax="false" value="Chỉnh sửa" icon="ui-icon ui-icon-pencil" action="#{permissionBean.prepareEdit()}"/>
                            </div>
                        </f:facet> 

                        <p:column headerText="Chức năng" sortBy="#{item.resourceId.name}">
                            #{item.resourceId.name}
                        </p:column>
                        <p:column headerText="Hành động" sortBy="#{item.action}">
                            #{item.action}
                        </p:column>
                        <p:column rendered="#{permissionBean.selectedCriteria == 'byResource'}" headerText="Người dùng">
                            <ui:repeat value="#{item.omapUserList}" var="user">
                                #{user.username} <br/>
                            </ui:repeat>
                        </p:column>
                        <p:column rendered="#{permissionBean.selectedCriteria == 'byResource'}" headerText="Nhóm">
                            <ui:repeat value="#{item.omapRoleList}" var="role">  
                                #{role.roleName} <br/>
                            </ui:repeat>
                        </p:column>
                    </p:dataTable>

                    <p:confirmDialog modal="true" message="Bạn muốn xóa các bản ghi đã chọn?"  
                                     hideEffect="explode"  
                                     header="Xác nhận" severity="alert" widgetVar="cauDeleteConfirm">  

                        <p:commandButton update="table" value="Có" oncomplete="cauDeleteConfirm.hide()"  
                                         action="#{permissionBean.remove()}"/>
                        <p:commandButton value="Không" onclick="cauDeleteConfirm.hide()" type="button" />   

                    </p:confirmDialog> 

                </h:form>
            </ui:define>

        </ui:composition>

    </body>
</html>
